﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.IO;

namespace YouHP.Pages.User
{
    public class Avatar : YouHP.Pages.User.UserBasePage
    {
      

        #region 页面变量

        /// <summary>
        /// 用户头像
        /// </summary>
        public string avatar;

        /// <summary>
        /// 用户头像地址
        /// </summary>
        public string avatarurl;

        /// <summary>
        /// 用户头像类型(来源）
        /// </summary>
        public int avatartype =0;

        public int Maxavatarwidth; 
        public int Maxavatarheight;
        public int Maxavatarsize = 200;

        /// <summary>
        /// 头像宽度
        /// </summary>
        public int avatarwidth;

        /// <summary>
        /// 头像高度
        /// </summary>
        public int avatarheight;

        /// <summary>
        /// 可用的模板列表
        /// </summary>
        public DataTable templatelist;

        /// <summary>
        /// 系统头像列表
        /// </summary>
        public DataTable AvatarfileList;

        protected new UserInfo User;

        #endregion


        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;
            this.SetTitle("头像设置");
            this.AddLocation("头像设置", "/User/Avatar.aspx");
            if (IsPost) HandlePost();
            else
            {

                AvatarfileList = UserBLL.Instance.GetAvatarList();
                User = UserBLL.Instance.GetUserInfo(UserData.Id);
            }
        }

        protected void HandlePost()
        {

           
              int avatartype = RequestTools.GetInt("avatartype", -1);
              if (avatartype != -1)
              {
                  switch (avatartype)
                  {
                      case 0: //从系统选择
                          avatar = RequestTools.GetString("usingavatar");
                          if (avatar != "")
                          {
                              avatar = Utils.UrlDecode(avatar.Substring(avatar.IndexOf("/support")));
                          }
                          //avatarwidth = 0;
                          //avatarheight = 0;
                          string path =Utils.GetMapPath(avatar);
                          if (!File.Exists(path))
                          {
                              AddErrLine("不存在的头像文件");
                              return;
                          }

                          break;

                      
                      case 1: //上传头像

                          if (UserGroupInfo.AllowAvatar < 3)
                          {
                              AddErrLine("您当前的身份 \"" + UserGroupInfo.Grouptitle + "\" 没有上传头像的权限");
                              return;
                          }
                          string str = RequestTools.GetFormString("file");
                          avatar = Utils.SaveRequestAvatarFile(UserData.Id, Maxavatarsize);
                          if (avatar.Equals(""))
                          {
                              AddErrLine(
                                  string.Format("头像图片不合法, 系统要求必须为gif jpg png图片, 大小不得超过 {2} KB",
                                                Maxavatarwidth, Maxavatarheight, Maxavatarsize));
                              return;
                          }
                          //Thumbnail thumb = new Thumbnail();
                          //if (!thumb.SetImage(avatar))
                          //{
                          //    AddErrLine("非法的图片格式");
                          //    return;
                          //}
                          //thumb.SaveThumbnailImage(Maxavatarwidth, Maxavatarheight);
                          avatarwidth = 0;
                          avatarheight = 0;
                          break;

                      case 2: //自定义头像Url

                          if (UserGroupInfo.AllowAvatar < 2)
                          {
                              AddErrLine(string.Format("您当前的身份 \"{0}\" 没有使用自定义头像的权限", UserGroupInfo.Grouptitle));
                              return;
                          }
                          avatar = RequestTools.GetString("avatarurl").Trim();
                          if (avatar.Length < 10)
                          {
                              AddErrLine("头像路径不合法");

                              return;
                          }
                          if (!avatar.Substring(0, 7).ToLower().Equals("http://"))
                          {
                              AddErrLine("头像路径必须以http://开始");
                              return;
                          }
                          string fileextname = Path.GetExtension(avatar).ToLower();
                          // 判断 文件扩展名/文件大小/文件类型 是否符合要求
                          if (
                              !(fileextname.Equals(".jpg") || fileextname.Equals(".gif") || fileextname.Equals(".png")))
                          {
                              AddErrLine("头像路径必须是.jpg .gif或.png结尾");
                              return;
                          }

                          //avatarwidth = RequestTools.GetInt("avatarwidth", Maxavatarwidth);
                          //avatarheight = RequestTools.GetInt("avatarheight", Maxavatarheight);
                          //if (avatarwidth <= 0 || avatarwidth > Maxavatarwidth || avatarheight <= 0 ||
                          //    avatarheight > Maxavatarheight)
                          //{
                          //    AddErrLine(
                          //        string.Format("自定义URL地址头像尺寸必须大于0, 且必须小于系统当前设置的最大尺寸 {0}x{1}", Maxavatarwidth,
                          //                      Maxavatarheight));

                          //    return;
                          //}
                          break;
                  }
              }
              else
              {
                  AddErrLine("请指定新头像的信息<br />");
                  return;
              }

             
              //更新用户图像

              UserInfo newUser = new UserInfo();
              newUser.Avatar = avatar;
              newUser.AvatarType = 0;
              newUser.Id = UserData.Id;
           
              bool succ = UserBLL.Instance.UpdateUserAvatar(newUser);
              if (succ)
              {

                  this.AddMsgLine("更新个人头像完毕！");
                  this.SetUrl("/User/Avatar.aspx");
                  this.SetMetaRefresh();
              }
              else
              {
                  this.AddErrLine("操作失败！");
                  return;
              }

        }

    }
}
